package com.hu.bgp.client;

import java.util.ArrayList;

import com.google.gwt.core.client.EntryPoint;
import com.google.gwt.core.client.GWT;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.user.client.rpc.ServiceDefTarget;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.ListBox;
import com.google.gwt.user.client.ui.RootPanel;
import com.google.gwt.user.client.ui.TextBox;
import com.hu.bgp.client.widgets.ScoreTable;
import com.hu.bgp.framework.util.FailureHandler;
import com.hu.bgp.framework.util.FailureHandlingCallback;
import com.hu.bgp.shared.BoardGameService;
import com.hu.bgp.shared.BoardGameServiceAsync;
import com.hu.bgp.shared.ClientGameType;
import com.hu.bgp.shared.ClientGameTypeCollection;
import com.hu.bgp.shared.ClientScore;

/**
 * De GUI om scores te bekijken.
 * 
 * @author ejvos
 */
public class ScoreClient implements EntryPoint, FailureHandler
{
	private BoardGameServiceAsync bgpService;
	private String currentGameTypeAlias;
	private final ListBox gameTypes = new ListBox();
	private final Button showScoresButton = new Button("Show scores");
	private final ScoreTable scoreTable = new ScoreTable();
	private final HorizontalPanel loginMessage = new HorizontalPanel();
	private final TextBox nameBox = new TextBox();
	private final Button loginButton = new Button("set name");
	private final Label nameLabel = new Label();

	public void onModuleLoad()
	{
		// get a binding to the servlet
		bgpService = GWT.create(BoardGameService.class);
		ServiceDefTarget target = (ServiceDefTarget) bgpService;
		// zie ook BgpConfiguration.getPlayerClientUrlPrefix
		target.setServiceEntryPoint("/bgp/bgpService");

		// grab a collection of gametypes
		bgpService.getClientGameTypeCollection(new FailureHandlingCallback<ClientGameTypeCollection>(this)
		{
			public void onSuccess(ClientGameTypeCollection result)
			{
				for (ClientGameType gameType : result)
				{
					gameTypes.addItem(gameType.getDisplayName(), gameType.getAlias());
				}
			}
		});

		// add some login controls
		loginMessage.add(nameLabel);
		loginMessage.add(nameBox);
		loginMessage.add(loginButton);

		bgpService.getProfileName(new FailureHandlingCallback<String>(ScoreClient.this)
		{
			public void onSuccess(String result)
			{
				if (result != null)
				{
					nameLabel.setText("Welcome back, " + result);
				}
			}
		});

		loginButton.addClickHandler(new ClickHandler()
		{
			public void onClick(ClickEvent event)
			{
				bgpService.setProfileName(nameBox.getText(), new FailureHandlingCallback<Void>(ScoreClient.this)
				{
					public void onSuccess(Void result)
					{
					}
				});
			}
		});

		showScoresButton.addClickHandler(new SelectGameTypeHandler());

		RootPanel.get("scoreTable").add(scoreTable);
		RootPanel.get("gameTypeSelector").add(gameTypes);
		RootPanel.get("gameTypeSelector").add(showScoresButton);
		RootPanel.get("loginMessage").add(loginMessage);
	}

	private class SelectGameTypeHandler implements ClickHandler
	{
		public void onClick(ClickEvent event)
		{
			currentGameTypeAlias = gameTypes.getValue(gameTypes.getSelectedIndex());
			gameTypeChanged();
		}
	}

	private void gameTypeChanged()
	{
		scoreTable.clear();

		// grab a collection of game instances
		bgpService.getScores(currentGameTypeAlias, new FailureHandlingCallback<ArrayList<ClientScore>>(this)
		{
			public void onSuccess(ArrayList<ClientScore> result)
			{
				for (ClientScore instance : result)
				{
					scoreTable.addScoreRow(instance);
				}
			}
		});
	}

	public void onFailure(Throwable caught)
	{
		caught.printStackTrace();
	}
}
